/*
 * @Author: your name
 * @Date: 2024-04-04 22:42:35
 * @LastEditTime: 2024-04-04 22:48:41
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\202.快乐数.cpp
 */
/*
 * @lc app=leetcode.cn id=202 lang=cpp
 *
 * [202] 快乐数
 */

// @lc code=start
class Solution {
public:
    int getSum(int n){
        int sum = 0;
        while(n){
            sum+= (n%10)*(n%10);
            n /=10;
        }
        return sum;
    }
    bool isHappy(int n) {
        if(n == 1) return true;
        int sum = n;
        unordered_set<int> set;
        while(1){
            sum = getSum(sum);
            if(sum == 1){
                break;
            }
            if(set.find(sum) != set.end()){
                return false;
            }
            else{
                set.insert(sum);
            }
        }
        return true;


    }
};
// @lc code=end
// 使用了set！不需要键值
